
/*********************************************************************************************************************************************
* CLASS NAME : BCBound
* 
* PURPOSE : 
*
* Enables basic collision detection , between sprites.I found unnecessary to implement calculations for rotated bounding boxes
* as in BattleCity sprites and projectiles only moves vertical or horizontal (axis aligned)
* - Features :
* - Bound vs bound intersection.
* - Bound vs bound raycast with hit point calculation (sweep test)
* - Bound vs vector (2d point) "is point inside" test
* - Bound vs vector (2d point) sweep test
*
* Derived From : This is a base class
**********************************************************************************************************************************************/


#ifndef BC_BOUND_H
#define BC_BOUND_H

#include "dependancies.h"
#include "BC_Vector2d.h"

class BCBound
{
	
	public :
		
		// constructs a BCBound (default constructor 0,0,0,0)
		BCBound();
		// constructs a BCBound from 4 points (left top right bottom)
		BCBound(int x1 , int y1 , int x2 , int y2);

		// constructs a BCBound from 2 vectors
		BCBound(const BCVector2d& leftTop , const BCVector2d& rightBottom);


		// setters :

		void set(const BCVector2d& leftTop , const BCVector2d& rightBottom);
		void set(int x1 , int y1 , int x2 , int y2);

		void set(const BCBound& bound);


		// getters :
		
		// returns true if the bound intersects another bound
		bool isIntersects(const BCBound& bound) const;

		// returns true if the point is inside the bound
		bool isPointInside(const BCVector2d& point) const;
		
		// returns true if the ray intersects the bound
		bool raytestBound(const BCVector2d& start , const BCVector2d& end) const;

		// returns true if the ray intersects the bound and returns the intersection point
		bool raytestBound(const BCVector2d& start , const BCVector2d& end , BCVector2d& resultPoint) const;
		
		// points defining the bound
		BCVector2d leftTop , rightBottom;
};





inline BCBound::BCBound():
						leftTop(0,0),
						rightBottom(0,0)

{
}

inline BCBound::BCBound(int x1, int y1, int x2, int y2) :
						leftTop(x1,y1),
						rightBottom(x2,y2)

{
}	





inline void BCBound::set(const BCVector2d& leftTop , const BCVector2d& rightBottom)
{
	this->leftTop = leftTop;
	this->rightBottom = rightBottom;
}




inline bool BCBound::isPointInside(const BCVector2d &point) const
{
	return ((point.x >= leftTop.x) && 
			(point.y >= leftTop.y) && 
			(point.x <= rightBottom.x) && 
			(point.y <= rightBottom.y));
	
}


inline bool BCBound::isIntersects(const BCBound &bound) const
{
	return !((bound.leftTop.x >= rightBottom.x) || 
			(bound.rightBottom.x <= leftTop.x) || 	
			(bound.leftTop.y >= rightBottom.y) || 
			(bound.rightBottom.y <= leftTop.y));
}


#endif